package com.wicket.okrfinance.biz.service.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wicket.okrfinance.biz.service.dto.common.MemoryBusinessResDataDto;
import com.wicket.okrfinance.common.util.CommonFunctionHelper;
import com.wicket.okrcomponent.common.baseinfo.BaseInfoHolder;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrfinance.biz.service.dto.common.MemoryBusinessDataDto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import com.wicket.okrfinance.biz.service.dto.req.ImplementSpaceConfPushMemoryFinanceReqDto;
import com.wicket.okrfinance.biz.service.dto.res.ImplementSpaceConfPushMemoryFinanceRespDto;
import com.wicket.okrfinance.biz.service.dto.req.QueryMemoryBusinessDataDetailComReqDto;
import com.wicket.okrfinance.biz.service.dto.res.QueryMemoryBusinessDataDetailComRespDto;
import com.wicket.okrfinance.biz.service.dto.req.AddMemoryBusinessSevenDataReqDto;
import com.wicket.okrfinance.biz.service.dto.res.AddMemoryBusinessSevenDataRespDto;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.wicket.okrfinance.biz.service.dto.req.ImplementPublicFieldPushMemoryReqDto;
import com.wicket.okrfinance.biz.service.dto.res.ImplementPublicFieldPushMemoryRespDto;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbInterfaceMode {
/**
      * code:publicFieldCache
      * name:M7执行空间配置推送内存（FINANCE）
      * desc:undefined
      * gen by moon at 5/19/2024, 2:41:02 PM
      * 
      **/
     @Trace(operationName = "M7执行空间配置推送内存（FINANCE）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementSpaceConfPushMemoryFinanceRespDto implementSpaceConfPushMemoryFinance(ImplementSpaceConfPushMemoryFinanceReqDto reqDto) {
         com.wicket.okrfinance.common.baseinfo.BaseInfoDO domain = new com.wicket.okrfinance.common.baseinfo.BaseInfoDO();
         if(BaseInfoHolder.contextHolder.get()!=null){
             BeanUtil.copyProperties(BaseInfoHolder.contextHolder.get().getBaseInfo(),domain);
             BaseInfoHolder.contextHolder.remove();
         }
         if(reqDto.getSpaceId()!=null){
             domain.setSpecSpaceId(reqDto.getSpaceId());
         }

         if(reqDto.getSpaceId()!=null){
             domain.setSpecInductionRecordId(reqDto.getSpaceId());
         }

         if(reqDto.getFileDisplayRootPath()!=null){
             domain.setFileDisplayRootPath(reqDto.getFileDisplayRootPath());
         }

         com.wicket.okrfinance.common.util.CommonFunctionHelper.setBaseInfoToLocal(domain);
         return new ImplementSpaceConfPushMemoryFinanceRespDto();
      }
/**
      * code:queryMemoryBusinessData
      * name:M7查询内存业务数据详情（特殊方法）
      * desc:undefined
      * gen by moon at 6/9/2024, 4:44:40 PM
      *
      **/
     @Trace(operationName = "M7查询内存业务数据详情（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public QueryMemoryBusinessDataDetailComRespDto queryMemoryBusinessDataDetailCom(QueryMemoryBusinessDataDetailComReqDto reqDto) {
         QueryMemoryBusinessDataDetailComRespDto retData = new QueryMemoryBusinessDataDetailComRespDto();
         String key = reqDto.getBusinessDataObject() + "_" +reqDto.getDataObjectBatchCode();
         JSONObject currentData = (JSONObject) CommonFunctionHelper.getBaseInfoFromLocal().getLocalCachePool().get(key);
         if(currentData==null){
             return retData;
         }

         Map<String, Field> fieldIndex = new HashMap<>();
         Field[] fields = QueryMemoryBusinessDataDetailComRespDto.class.getDeclaredFields();
         for(Field one : fields){
             one.setAccessible(true);
             fieldIndex.put(one.getName(),one);
         }


         for(MemoryBusinessResDataDto one : reqDto.getMemoryBusinessResDataList()){
             Object value = currentData.get(one.getFieldName());
             Field field = fieldIndex.get(one.getMapFieldName());
             if(value!=null){
                 try {
                     field.set(retData,String.valueOf(value));
                 } catch (IllegalAccessException e) {
                     e.printStackTrace();
                 }
             }
         }
         return retData;
      }
/**
      * code:addMemoryBusinessData
      * name:M7新增内存业务数据（特殊方法）
      * desc:undefined
      * gen by moon at 6/9/2024, 4:45:24 PM
      *
      **/
     @Trace(operationName = "M7新增内存业务数据（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public AddMemoryBusinessSevenDataRespDto addMemoryBusinessSevenData(AddMemoryBusinessSevenDataReqDto reqDto) {
         com.wicket.okrfinance.common.baseinfo.BaseInfoDO domain = new com.wicket.okrfinance.common.baseinfo.BaseInfoDO();
         if(com.wicket.okrfinance.common.baseinfo.BaseInfoHolder.contextHolder ==null
          || com.wicket.okrfinance.common.baseinfo.BaseInfoHolder.contextHolder.get()==null
         || com.wicket.okrfinance.common.baseinfo.BaseInfoHolder.contextHolder.get().getBaseInfo()==null){
             com.wicket.okrfinance.common.util.CommonFunctionHelper.setBaseInfoToLocal(domain);
         }

         String key = reqDto.getBusinessDataObject() + "_" +reqDto.getDataObjectBatchCode();
         JSONObject objectData = new JSONObject();
         for(MemoryBusinessDataDto one : reqDto.getMemoryBusinessDataList()){
             objectData.put(one.getFieldName(),one.getFieldValue());
         }
         if(reqDto.getIsDataList().equals("TRUE")){
             Object currentData = CommonFunctionHelper.getBaseInfoFromLocal().getLocalCachePool().get(key);
             if(currentData!=null){
                 if(currentData instanceof JSONArray){
                     JSONArray arrayData = (JSONArray)currentData;
                     arrayData.add(objectData);
                     CommonFunctionHelper.getBaseInfoFromLocal().getLocalCachePool().put(key,arrayData);
                 }else{
                     throw new BizException("-1","想取数据集，但是存的是对象",false);
                 }
             }else{
                 JSONArray arrayData = new JSONArray();
                 arrayData.add(objectData);
                 CommonFunctionHelper.getBaseInfoFromLocal().getLocalCachePool().put(key,arrayData);
             }
         }else{
             CommonFunctionHelper.getBaseInfoFromLocal().getLocalCachePool().put(key,objectData);
         }
         return new AddMemoryBusinessSevenDataRespDto();
      }
/**
      * code:publicFieldCache
      * name:M7执行底层框架公共字段推送内存（特殊方法）
      * desc:undefined
      * gen by moon at 3/12/2025, 5:31:05 PM
      *
      **/
     @Trace(operationName = "M7执行底层框架公共字段推送内存（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementPublicFieldPushMemoryRespDto implementPublicFieldPushMemory(ImplementPublicFieldPushMemoryReqDto reqDto) {
         com.wicket.okrfinance.common.baseinfo.BaseInfoDO domain = new com.wicket.okrfinance.common.baseinfo.BaseInfoDO();
         if(BaseInfoHolder.contextHolder.get()!=null){
             BeanUtil.copyProperties(BaseInfoHolder.contextHolder.get().getBaseInfo(),domain);
             BaseInfoHolder.contextHolder.remove();
         }
         if(reqDto.getSpaceId()!=null){
             domain.setSpecSpaceId(reqDto.getSpaceId());
         }

         if(reqDto.getOperationInductionId()!=null){
             domain.setSpecInductionRecordId(reqDto.getOperationInductionId());
         }

         if(reqDto.getAppId()!=null){
             domain.setSpecAppId(reqDto.getAppId());
         }

         com.wicket.okrfinance.common.util.CommonFunctionHelper.setBaseInfoToLocal(domain);
         return new ImplementPublicFieldPushMemoryRespDto();
      }
  //手工接入方法
}
